package com.im.linving.imbll;


import com.im.linving.RegisterBody;
import com.im.linving.ResForRt;
import com.im.linving.apputils.AppTools;
import com.im.linving.apputils.CheckSumBuilder;
import com.im.linving.imapidal.UserDal;

import com.im.linving.apputils.MD5;
import com.im.linving.apputils.functions.Action1;
import com.im.linving.apputils.functions.Action2;
import rx.Observer;
import rx.Scheduler;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * @Author linyong
 * @Date 2016/10/31
 * @Time 17:57
 */
public class UserBll {

    private UserDal userDal = new UserDal();

    /**
     * 注册
     *
     * @param name
     * @param pwd
     * @param success
     * @param fail
     */
    public void register(String name, String pwd, final Action1<Object> success, final Action2<Integer, String> fail) {
        String appKey = AppTools.getAppKey();
        String nonce = "ydcq";
        String curTime = System.currentTimeMillis()+"";
        String checkSum = CheckSumBuilder.getCheckSum(AppTools.getAppSecret(),nonce,curTime);
        String accid = name;
        String props = "--";
        String token = MD5.getStringMD5(pwd);

        RegisterBody registerBody = new RegisterBody();
        registerBody.setAccid(accid);
        registerBody.setProps(props);
        registerBody.setToken(token);

        userDal.register(appKey, nonce,
                curTime, checkSum, accid,props,token)
                .subscribeOn(Schedulers.io()) // 指定 subscribe() 发生在 IO 线程
                .observeOn(AndroidSchedulers.mainThread()) // 指定 Subscriber 的回调发生在主线程
                .subscribe(new Observer<ResForRt<Object>>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                fail.call(-1,"Throwable" + e.toString());
                e.printStackTrace();
            }

            @Override
            public void onNext(ResForRt<Object> resForRt) {
              if(resForRt.getCode() == 200){
                  
                  success.call(resForRt.getDesc());
              }else {
                  fail.call(resForRt.getCode(),resForRt.getDesc());
              }
            }
        });

    }


}
